/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Entity.Test;
import Entity.TestFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(
        name = "controlller",
        loadOnStartup = 1,
        urlPatterns = {"/controlller","/themmoi","/themhv","/chinhsua","/xoa","/valid"}
        ) 
public class controlller extends HttpServlet {
    @EJB
    private TestFacade testFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet controlller</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>ShihServlet controlller at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");*/
            
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();
         PrintWriter out = response.getWriter();
        if(path.equals("/xoa")){
            int id = Integer.parseInt(request.getParameter("id"));
            Test hocvien = testFacade.find(id);           
            testFacade.remove(hocvien);           
            String d = request.getContextPath();
           // response.getWriter().close();
            response.sendRedirect("");
            return;
            
        }else if(path.equals("/chinhsua")){
            int id = Integer.parseInt(request.getParameter("id"));
            Test hocvien = testFacade.find(id);
            request.setAttribute("HocVien", hocvien);
            request.getRequestDispatcher("WEB-INF/view/suahv.jsp").forward(request, response);
            return;
            
        }else if(path.equals("/valid")){
            
            int CMND = Integer.parseInt(request.getParameter("CMND"));
            Test t = testFacade.findByCMND(CMND);
            if(t==null){
                out.write("ok");
                return;
            }else{                
                return;
            }
        }
        
        List<Test> dshv = testFacade.findAll();
        request.setAttribute("DanhSachHocVien", dshv); 
        request.getRequestDispatcher("WEB-INF/view/danhsach.jsp").forward(request, response);
         
        //processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String path = request.getServletPath();
        PrintWriter out = response.getWriter();
        if(path.equals("/themmoi")){
            request.getRequestDispatcher("WEB-INF/view/themhv.jsp").forward(request, response);
        }else if(path.equals("/themhv")){
            String Name = request.getParameter("Ten");
            String NgaySinh = request.getParameter("NgaySinh");
            int CMND = Integer.parseInt(request.getParameter("CMND"));
            Test hocvien = new Test();
            hocvien.setName(Name);
            hocvien.setNgaysinh(new Date(NgaySinh));            
            hocvien.setCmnd(CMND);
            String ID = request.getParameter("IdTest");
            if(ID!=null){
                hocvien.setIdtest(Integer.parseInt(ID));
                testFacade.edit(hocvien);
            }else{
                hocvien.setIdtest(0);
                testFacade.create(hocvien);
            }
            
            
            response.sendRedirect("");
            //request.getRequestDispatcher("WEB-INF/view/danhsach.jsp").forward(request, response);
            
        }
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
